package com.wanmait.recycle.webcontroller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wanmait.recycle.pojo.Userinfo;
import com.wanmait.recycle.pojo.Usertype;
import com.wanmait.recycle.service.UserinfoService;
import com.wanmait.recycle.service.UsertypeService;
import com.wanmait.recycle.util.EmailSend;

@Controller
@RequestMapping("web/")
public class UserYanZhengController {
	
	@Resource
	private UserinfoService userinfoService;
	@Resource
	private UsertypeService usertypeService;

	@RequestMapping("userinfo/yanZheng")
	public ModelAndView yanZheng(HttpSession session)
	{ 
		ModelAndView mv = new ModelAndView();
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		if(userinfo==null) {
			mv.addObject("msg","login");
			mv.setViewName("redirect:/index.html");
			return mv;
		}
		
		Userinfo users = userinfoService.findAllById(userinfo.getId());
		List<Usertype> usertypes = usertypeService.findAll();
		String phone = null;
		String phoneTwo = null;
		String phoneNew = null;
		String phoneThree = null;
		try {
			phone = users.getPhone();
			phoneTwo = phone.substring(0,3);
			phoneThree = phone.substring(7);
			phoneNew = phoneTwo+"*****"+phoneThree;
			
			String mailBox = users.getMailBox();
			String[] str = mailBox.split("@");
			String str1 = str[0].substring(0, 5);
			String newMail = str1 + "*******@"+str[1];
			users.setMailBox(newMail);
		} catch (Exception e) {
			phoneNew = "还未设置手机号";
			
		}
		
		mv.addObject("phoneNew", phoneNew);
		mv.addObject("users", users);
		mv.addObject("usertypes", usertypes);
		return mv;
	}
	
	@RequestMapping("userinfo/checkMail")
	@ResponseBody
	public String checkMail(String mailBox)
	{
		
		String code;
		EmailSend emailSend = EmailSend.getEmailSend();
		try {
			code=emailSend.sendEmail(mailBox);
			return code;
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping("userinfo/updateMailById")
	public String updateMailById(HttpSession session, String newemail)
	{
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		
		userinfoService.updateMailById(userinfo.getId(), newemail);
		
		return "redirect:/web/userinfo/yanZheng";
	}
	
	@RequestMapping("userinfo/updatePhoneById")
	public String updatePhoneById(HttpSession session, String newPhone)
	{
		
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		
		userinfoService.updatePhoneById(userinfo.getId(), newPhone);
		
		return "redirect:/web/userinfo/yanZheng";
	}
	
	@RequestMapping("userinfo/updateHeadNameById")
	public String updateHeadNameById(HttpSession session, String headName)
	{
		
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		userinfoService.updateHeadNameById(userinfo.getId(), headName);
		
		Userinfo user = (Userinfo)userinfoService.selectByPrimaryKey(userinfo.getId());
		session.setAttribute("userinfo", user);
		return "redirect:/web/userinfo/yanZheng";
	}
	
	@RequestMapping("userinfo/updateCompanyById")
	public String updateCompanyById(Userinfo userinfo,HttpSession session)
	{	
		userinfoService.updateCompanyById(userinfo);
		return "redirect:/web/userinfo/yanZheng";
	}
	
	@RequestMapping("userinfo/updatePasswordById")
	public String updatePasswordById(HttpSession session, String newPass2)
	{
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		userinfoService.updatePasswordById(userinfo.getId(), newPass2);
		return "redirect:/web/userinfo/yanZheng";
	}
	
	

	@RequestMapping("userinfo/getPassword")
	@ResponseBody
	public boolean getPassword(String password,HttpSession session)
	{
		Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
		userinfo.setPassword(password);
		Userinfo user = userinfoService.findUserinfoByExample(userinfo);
	
		if(user==null) {
			return false;
		}else {
			return true;
		}
		
		
		
	}
	
}


